package com.fr.schedule.job;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.xml.BaseXMLUtils;
import com.fr.report.ResultWorkBook;
import com.fr.report.TemplateWorkBook;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.schedule.ScheduleContext;
import com.fr.schedule.ViewScheduleSavedInfo;
import com.fr.schedule.dao.JdbcDaoManager;
import com.fr.schedule.output.EmailException;
import com.fr.schedule.output.EmailNotification;
import com.fr.schedule.output.FileRepository;
import com.fr.schedule.output.ScheduledOutput;
import com.fr.schedule.task.ScheduleTask;
import com.fr.schedule.util.ScheduleUtils;
import com.fr.third.org.quartz.Job;
import com.fr.third.org.quartz.JobDataMap;
import com.fr.third.org.quartz.JobExecutionContext;
import com.fr.third.org.quartz.JobExecutionException;
import com.fr.util.Utils;
import com.fr.web.Reportlet;
import com.fr.web.ReportletException;
import com.fr.web.ReportletRequest;
import com.fr.web.core.WebUtils;
import com.fr.web.platform.entry.ReportletEntry;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ViewReportletJob
  implements Job
{
  public void execute(JobExecutionContext paramJobExecutionContext)
    throws JobExecutionException
  {
    Date localDate = paramJobExecutionContext.getScheduledFireTime();
    JobDataMap localJobDataMap = paramJobExecutionContext.getMergedJobDataMap();
    long l1 = localJobDataMap.getLong("__task__");
    ScheduleTask localScheduleTask = (ScheduleTask)ScheduleContext.getJdbcDaoManager().load(ScheduleTask.class, l1);
    FileRepository localFileRepository = localScheduleTask.getScheduledOutput().getFileRepository();
    File localFile = localFileRepository.getTaskTimeDirFile(localScheduleTask.getName(), localDate);
    if (!(localFile.exists()))
      Utils.mkdirs(localFile);
    ScheduledOutput localScheduledOutput = localScheduleTask.getScheduledOutput();
    long l2 = localJobDataMap.getLong("__reportlet__");
    ReportletEntry localReportletEntry = (ReportletEntry)ScheduleContext.getJdbcDaoManager().load(ReportletEntry.class, l2);
    String str1 = localReportletEntry.getReportletPath();
    if (str1 == null)
    {
      FRContext.getLogger().log(Level.WARNING, Inter.getLocText("Report path is Null..."));
      return;
    }
    FRContext.getLogger().log(Level.INFO, "The Task '" + localScheduleTask.getName() + "' of " + localReportletEntry.getDisplayName() + " starts!");
    String str2 = str1.substring(str1.lastIndexOf("/") + 1, str1.lastIndexOf("."));
    ArrayList localArrayList = new ArrayList();
    try
    {
      Reportlet localReportlet = WebUtils.initReportletFromPath(str1);
      if (localReportlet == null)
        FRContext.getLogger().log(Level.WARNING, "Not found reportlet: " + str1);
      Calendar localCalendar = Calendar.getInstance();
      localCalendar.setTimeInMillis(localDate.getTime());
      Calculator localCalculator = Calculator.createCalculator();
      localCalculator.set("$date", localCalendar.getTime());
      TemplateWorkBook localTemplateWorkBook1 = localReportlet.createReport(null);
      localCalculator.setCurrentReport(localTemplateWorkBook1.getReport(0));
      localCalculator.setCurrentTableDataSource(localTemplateWorkBook1);
      Object[] arrayOfObject = ScheduleUtils.createCartesianParameterMapList(localScheduleTask.getParametersToArray(), localCalculator);
      List localList1 = (List)arrayOfObject[0];
      localCalculator.release();
      ViewScheduleSavedInfo localViewScheduleSavedInfo = new ViewScheduleSavedInfo();
      List localList2 = (List)arrayOfObject[1];
      if (localList2.size() > 0)
        localViewScheduleSavedInfo.setFArrayParameters((Parameter[])localList2.toArray(new Parameter[localList2.size()]));
      String[] arrayOfString = new String[localList2.size()];
      int i = 0;
      int j = arrayOfString.length;
      while (i < j)
      {
        arrayOfString[i] = ((Parameter)localList2.get(i)).getName();
        ++i;
      }
      for (i = 0; i < localList1.size(); ++i)
      {
        Map localMap = (Map)localList1.get(i);
        TemplateWorkBook localTemplateWorkBook2 = null;
        try
        {
          localTemplateWorkBook2 = localReportlet.createReport(new ScheduleReportletRequestImpl(localMap));
        }
        catch (ReportletException localReportletException)
        {
          FRContext.getLogger().log(Level.WARNING, Inter.getLocText("Not_Found_Reportlet") + ": " + str1);
        }
        if (localTemplateWorkBook2 == null)
        {
          FRContext.getLogger().log(Level.WARNING, Inter.getLocText("Not_Found_Reportlet") + ": " + str1);
          return;
        }
        ResultWorkBook localResultWorkBook = localTemplateWorkBook2.execute(localMap);
        String str3 = createParaString(i, arrayOfString);
        File[] arrayOfFile = localScheduledOutput.exportReportFiles(localResultWorkBook, str2 + str3, localFile);
        for (int k = 0; k < arrayOfFile.length; ++k)
        {
          localViewScheduleSavedInfo.addMapTemplateFileName(localMap, arrayOfFile[k].getName());
          if (!(arrayOfFile[k].getName().endsWith(".cpt")))
            localArrayList.add(arrayOfFile[k]);
        }
      }
      exportViewScheduleSavedInfo(localViewScheduleSavedInfo, localFile);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      return;
    }
    EmailNotification localEmailNotification = localScheduledOutput.getEmailNotification();
    if (localEmailNotification != null)
      try
      {
        localEmailNotification.sendEmail((File[])localArrayList.toArray(new File[localArrayList.size()]));
      }
      catch (EmailException localEmailException)
      {
        FRContext.getLogger().log(Level.WARNING, localEmailException.getMessage(), localEmailException);
      }
    FRContext.getLogger().log(Level.INFO, "The Task '" + localScheduleTask.getName() + "' of " + localReportletEntry.getDisplayName() + " has finished!");
  }

  private String createParaString(int paramInt, String[] paramArrayOfString)
  {
    if (paramArrayOfString.length < 1)
      return "";
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("_[");
    int i = 0;
    int j = paramArrayOfString.length;
    while (i < j)
    {
      localStringBuffer.append(paramArrayOfString[i]).append("_").append(paramInt);
      if (i < j - 1)
        localStringBuffer.append(",");
      ++i;
    }
    localStringBuffer.append("]");
    return localStringBuffer.toString();
  }

  private static void exportViewScheduleSavedInfo(ViewScheduleSavedInfo paramViewScheduleSavedInfo, File paramFile)
    throws FileNotFoundException
  {
    BaseXMLUtils.writeXMLFile(new FileOutputStream(new File(paramFile, "info.xml")), paramViewScheduleSavedInfo);
  }

  private static class ScheduleReportletRequestImpl
    implements ReportletRequest
  {
    private Map map = null;

    public ScheduleReportletRequestImpl(Map paramMap)
    {
      this.map = paramMap;
    }

    public Object getParameter(String paramString)
    {
      return this.map.get(paramString);
    }
  }
}